package com.xiaohao;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.File;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2019-01-11 20:33
 * @package com.xiaohao
 * @description
 */
public class AppUI {
    private JButton button;
    private JPanel panel;
    private JTextPane textPane1;
    private JButton tianjiaButton;
    private JButton qingkongButton;
    private JButton jieyaButton;
    private JLabel tianjia;
    private JLabel jieya;
    private JLabel qingkong;
    private JButton wenjianjiaButton;
    private JLabel wenjianjia;
    private Robot robot = new Robot();
    private  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    private Location qingkongLo=new Location();
    private Location tianjiaLo=new Location();
    private Location jieyaLo=new Location();
    private Location wenjianjiaLo=new Location();
    public AppUI() throws AWTException {
        JFrame frame = new JFrame("批量操作");
        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(500, 500);

        addLocation(tianjiaButton,tianjiaLo, tianjia);
        addLocation(qingkongButton,qingkongLo,qingkong);
        addLocation(jieyaButton,jieyaLo,jieya);
        addLocation(wenjianjiaButton,wenjianjiaLo,wenjianjia);



        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fileDialog = new FileDialog(frame, "打开文件", FileDialog.LOAD);
                fileDialog.setVisible(true);

                String dirName = fileDialog.getDirectory();
                String fileName = fileDialog.getFile();
                //读取展示文件
                if (dirName == null || fileName == null) {
                    return;
                }
                File[] files = new File(dirName).listFiles();
                autoWork(files);
            }
        });
    }

    public static void main(String[] args) {
        try {
            new AppUI();
        } catch (AWTException e) {
            System.out.println("系统不支持！");
            e.printStackTrace();
        }
    }


    public void autoWork(File[] num) {
        textPane1.setText("");
        for (int i=0;i<num.length;i++){
            //清空
            robot.mouseMove(qingkongLo.getX(),qingkongLo.getY());
            robot.mousePress(MouseEvent.BUTTON1_MASK);
            robot.mouseRelease(MouseEvent.BUTTON1_MASK);
            robot.delay(1000);

            Transferable transferable = new StringSelection(num[i].getAbsolutePath());
            clipboard.setContents(transferable, null);


            //添加
            robot.mouseMove(tianjiaLo.getX(),tianjiaLo.getY());

            robot.mousePress(MouseEvent.BUTTON1_MASK);
            robot.mouseRelease(MouseEvent.BUTTON1_MASK);
            robot.delay(1000);
            //粘贴
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);

            robot.delay(1000);

            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);

            robot.delay(1000);

            //选择解压文件夹
            robot.mouseMove(jieyaLo.getX(), jieyaLo.getY());
            robot.mousePress(MouseEvent.BUTTON1_MASK);
            robot.mouseRelease(MouseEvent.BUTTON1_MASK);

            robot.delay(1200);

            robot.mouseMove(wenjianjiaLo.getX(),wenjianjiaLo.getY());
            robot.mousePress(MouseEvent.BUTTON1_MASK);
            robot.mouseRelease(MouseEvent.BUTTON1_MASK);

            robot.delay(500);

            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            robot.delay(1300);

            textPane1.setText(textPane1.getText()+num[i]+"解压成功\n");
            textPane1.paintImmediately(textPane1.getBounds());
        }
        textPane1.setText(textPane1.getText()+"成功解压"+num.length+"个");
        textPane1.paintImmediately(textPane1.getBounds());
    }

    public void addLocation(JButton button,Location location,JLabel jLabel){
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                Point point = MouseInfo.getPointerInfo().getLocation();
                location.setX(point.x);
                location.setY(point.y);
                jLabel.setText(location.getX()+"   "+location.getY());
            }
        });

    }
}
